<?php
include '../../../wp-blog-header.php';
$brand_id = '942';
$url = 'http://123loot.com/rss_feed/rss.aspx';
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
$loot_deals = xml2array($curl_json_output);

$loot_deals = $loot_deals['rss']['channel']['item'];


//echo '<pre>';
//print_r($loot_deals);
//echo '</pre>';
//exit();



foreach($loot_deals as $key => $value)
{
$columns_deal['name'] = pods_sanitize($value['title']);
$columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
//$columns_deal['short'] = pods_sanitize($value['offer_description']);
$ext_img_url = $value['Imageurl'];
 
$deal = new Pod('deal');
$where = "t.camp_link = '".$columns_deal['camp_link']."'";
$deal->findRecords('id DESC', 1,$where);
$dealsFound = $deal->getTotalRows();
//echo $dealsFound; 

if($dealsFound == 0 && !empty($value['link']))
{
  unset($ids);
  unset($current_cities);
  $City = new Pod('city');
	$City->findRecords('name ASC', -1);
	while($City->fetchRecord())
	{
  	$ids  = $City->get_field('id');
  	$current_cities[] = $ids;
	}
  $current_cities = implode(',',$current_cities);
  $columns_deal['city'] = $current_cities;

  $shortened_url = make_bitly_url($value['link']);
  if(empty($shortened_url))
  {
    $shortened_url = make_goog_url($value['link']);
  }
  $columns_deal['extlink'] = $shortened_url;
  
  
  $columns_deal['camp_end'] = date('Y-m-d H:i:s',strtotime($value['EndDate']));
  $columns_deal['camp_start'] = date('Y-m-d H:i:s',strtotime($value['StartDate']));
  $columns_deal['brand'] = $brand_id;
  $columns_deal['daily'] = '1';
  $columns_deal['oriprice'] = $value['Originalprice'];
  $columns_deal['disper'] = $value['Discountedpercentage'];
  $columns_deal['disprice'] = $value['Discountedprice'];
  $columns_deal['ext_image_url'] = $ext_img_url;

  $cat_id = check_daily_category($columns_deal['name']);
  if(!empty($cat_id))
  {
    $columns_deal['camp_cat'] = $cat_id;
  }

  $api = new PodAPI();
  $params = array('datatype' => 'deal', 'columns' => $columns_deal);
  
  /*
  echo '<pre>';
  print_r($params);
  echo '</pre>';
  exit();
  */
  $api->save_pod_item($params);

}

}
